package com.rms.recruit_management_system.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rms.recruit_management_system.mapper.AdministratorMapper;
import com.rms.recruit_management_system.pojo.Administrator;
import com.rms.recruit_management_system.service.AdministratorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class AdministratorServiceImpl extends ServiceImpl<AdministratorMapper, Administrator> implements AdministratorService{
    @Autowired
    private AdministratorMapper administratorMapper;

    /**
     * 管理员登录
     * @param administrator
     * @return
     */
    @Override
    public Administrator adminLogin(Administrator administrator) {
         return administratorMapper.selectOne(new QueryWrapper<Administrator>()
                    .eq("username", administrator.getUsername())
                    .eq("password", MD5.create().digestHex(administrator.getPassword())));
    }

    /**
     * 根据管理员id查询管理员信息
     * @param adminId
     * @return
     */
    @Override
    public Administrator findAdminByAdminId(Integer adminId) {
        return administratorMapper.selectById(adminId);
    }

    /**
     * 获取管理员信息列表
     * @return
     */
    @Override
    public List<Administrator> getAdministratorList() {
        LambdaQueryWrapper<Administrator> wrapper=new LambdaQueryWrapper<>();
        wrapper.ne(Administrator::getRole,"super_admin");
        return administratorMapper.selectList(wrapper);
    }

    /**
     * 更新管理员信息
     * @param administrator
     */
    @Override
    public void updateAdministrator(Administrator administrator) {
        administratorMapper.updateById(administrator);
    }

    /**
     * 添加管理员信息
     * @param administrator
     */
    @Override
    public void addAdministrator(Administrator administrator) {
        try {
            administrator.setCreateTime(LocalDateTime.now());
            administrator.setPassword(MD5.create().digestHex(administrator.getPassword()));
            administratorMapper.insert(administrator);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 删除管理员信息
     * @param adminId
     */
    @Override
    public void deleteAdministrator(Integer adminId) {
        administratorMapper.deleteById(adminId);
    }

    /**
     * 批量删除管理员信息
     * @param adminIdList
     */
    @Override
    public void batchDeleteAdministrator(List<Integer> adminIdList) {
        administratorMapper.deleteBatchIds(adminIdList);
    }

    /**
     * 设置管理员最后登录时间
     * @param adminId
     */
    @Override
    public void setLastLoginTime(Integer adminId) {
        administratorMapper.update(null,new UpdateWrapper<Administrator>().set("last_login_time", LocalDateTime.now()).eq("admin_id", adminId));
    }

    /**
     * 更新管理员密码
     * @param adminId
     * @param password
     */
    @Override
    public void updatePassword(Integer adminId, String password) {
        administratorMapper.update(null,new UpdateWrapper<Administrator>().set("password", MD5.create().digestHex16(password)).eq("admin_id", adminId));
    }
}
